package com.start.admin.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.start.admin.entity.StartSysDept;
import com.start.admin.service.StartSysDeptService;
import com.start.common.api.Result;
import com.start.common.utils.TreeModelUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/start/sysDept")
public class StartSysDeptController {
    @Autowired
    private StartSysDeptService startSysDeptService;

    @GetMapping("/list")
    public Result list(StartSysDept entity) {
        LambdaQueryWrapper<StartSysDept> lambdaQuery = Wrappers.lambdaQuery(entity);
        return Result.ok(startSysDeptService.list(lambdaQuery));
    }
    @GetMapping("/listTree")
    public Result listTree(StartSysDept entity) {
        LambdaQueryWrapper<StartSysDept> lambdaQuery = Wrappers.lambdaQuery(entity);
        List<StartSysDept> startSysDepts = startSysDeptService.list(lambdaQuery);
        return Result.ok(TreeModelUtils.createTree(startSysDepts));
    }

    @PostMapping("/add")
    @PreAuthorize("@pas.hasPermission('sys:dept:add')")
    public Result add(@RequestBody StartSysDept entity) {
        startSysDeptService.createEntity(entity);
        return Result.ok();
    }

    @PutMapping("/updateById")
    @PreAuthorize("@pas.hasPermission('sys:dept:edit')")
    public Result updateById(@RequestBody StartSysDept entity) {
        startSysDeptService.updateById(entity);
        return Result.ok();
    }

    @GetMapping("/getById/{id}")
    @PreAuthorize("@pas.hasPermission('sys:dept:info')")
    public Result getById(@PathVariable("id") String id) {
        return Result.ok(startSysDeptService.getById(id));
    }


    @DeleteMapping("deleteById/{id}")
    @PreAuthorize("@pas.hasPermission('sys:dept:delete')")
    public Result<?> deleteById(@PathVariable("id") String id) {
        startSysDeptService.removeById(id);
        return Result.ok();
    }
}
